<template>
   
  <div>
       
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      size="mini"
      label-width="80px"
      align="left"
    >
           
      <el-form-item label="用户昵称:">
                <el-input v-model="formInline.name"></el-input>      
      </el-form-item>
           
      <el-form-item label="还款时间:">
               
        <el-date-picker
          v-model="formInline.date"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
        >
                 
        </el-date-picker>
             
      </el-form-item>
           
      <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>    
            <el-button type="info" @click="cancel">取消</el-button>      
      </el-form-item>
         
    </el-form>
       
    <el-table
      :data="afterLoan"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
            <el-table-column type="selection" width="55"> </el-table-column>    
       
      <el-table-column prop="uname" label="用户昵称" width="120" align="center">
             
      </el-table-column>
           
      <el-table-column
        prop="dueDate"
        label="应还日期"
        width="120"
        align="center"
      >
             
      </el-table-column>
           
      <el-table-column label="应还金额" width="120" align="center">
               
        <template slot-scope="scope">
                   
          <span>{{ (scope.row.should / scope.row.loanDate).toFixed(2) }}</span>
                 
        </template>
             
      </el-table-column>
           
      <el-table-column prop="penalty" label="罚息" width="100" align="center">
             
      </el-table-column>
           
      <el-table-column
        prop="periods"
        label="还款期数"
        width="100"
        align="center"
      >
             
      </el-table-column>
           
      <el-table-column prop="email" label="邮箱" width="160" align="center">
             
      </el-table-column>
           
      <el-table-column
        prop="overdueDays"
        label="逾期天数"
        width="100"
        align="center"
      >
             
      </el-table-column>
           
      <el-table-column label="操作" style="margin-left: 50px">
               
        <template slot-scope="scope">
                   
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
                   
          <el-button size="mini" @click="SendNotification(scope.row)"
            >催款</el-button
          >
                 
        </template>
             
      </el-table-column>
         
    </el-table>
       
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="current"
      layout="prev, pager, next"
      :total="total"
      :page-size="3"
    >
         
    </el-pagination>
     
  </div>
</template>
<script>
export default {
  data() {
    return {
      current: 1,
      total: 100,
      formInline: {
        name: "",
        date: [],
      },
      afterLoan: [],
      multipleSelection: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  methods: {
    onSubmit() {
      let afterLoans = {
        uname: this.formInline.name,
        date: this.formInline.date,
        current: this.current,
      };
      this.$axios
        .post("http://localhost:8601/afterLoan/list", afterLoans)
        .then((r) => {
          if (r.data.code == 200) {
            this.total = r.data.data.total;
            this.afterLoan = r.data.data.records;
          }
        });
    },
    cancel() {
      this.formInline = {
        uname: "",
        date: [],
      };
      this.$axios
        .post("http://localhost:8601/afterLoan/list", {
          overdue: "y",
          current: this.current,
        })
        .then((r) => {
          if (r.data.code == 200) {
            this.total = r.data.data.total;
            this.afterLoan = r.data.data.records;
          }
        });
    },
    handleDelete(afterLoan) {
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let multipleSelection = [];
          if (afterLoan != null) {
            multipleSelection.push(afterLoan);
          } else {
            multipleSelection = this.multipleSelection;
          }
          this.$axios
            .post("http://localhost:8601/afterLoan/delete", multipleSelection)
            .then((r) => {
              if (r.data.code == 200) {
                this.$axios
                  .post("http://localhost:8601/afterLoan/list", {
                    overdue: "y",
                  })
                  .then((r) => {
                    if (r.data.code == 200) {
                      this.total = r.data.data.pages;
                      this.afterLoan = r.data.data.records;
                    }
                  });
              }
            }),
            this.$message({
              type: "success",
              message: "删除成功!",
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleCurrentChange(val) {
      this.current = val;
      this.onSubmit();
    },
    SendNotification(afterLoans) {
      console.log(afterLoans);
      this.$prompt("请输入发送内容", "提示", {
        inputValue:
          "尊敬的用户您当前已逾期,现罚息" +
          afterLoans.penalty +
          "元,请尽快还款",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          afterLoans.letterContent = value;
          let multipleSelection = [];
          multipleSelection.push(afterLoans);
          this.$axios
            .post("http://localhost:8601/afterLoan/mail", multipleSelection)
            .then((r) => {
              if (r.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "发送成功",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
  },
  mounted() {
    this.$axios
      .post("http://localhost:8601/afterLoan/list", { overdue: "y" })
      .then((r) => {
        if (r.data.code == 200) {
          this.total = r.data.data.total;
          console.log(r.data.data);
          this.afterLoan = r.data.data.records;
        }
      });
  },
};
</script>
